﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
namespace UIT_CV_Management
{
    public class TieuLuan
    {
        public String maTieuLuan;
        private String tenTieuLuan;
        private DateTime ngayBaoVe;
        private String noiBaoVe;
        private float ketQuaBaoVe;
        private String nguoiHuongDan;
        private Database myDatabase = Database.shareDatabase;

        public TieuLuan(String _tenTieuLuan, DateTime _ngayBaoVe, String _noiBaoVe, float _ketquaBaoVe, String _nguoiHuongDan)
        {
            maTieuLuan = Database.getID("TieuLuan", "MaTieuLuan");

            tenTieuLuan = _tenTieuLuan;
            ngayBaoVe = _ngayBaoVe;
            noiBaoVe = _noiBaoVe;
            ketQuaBaoVe = _ketquaBaoVe;
            nguoiHuongDan = _nguoiHuongDan;
        }

        public TieuLuan(String _maTieuLuan)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM TieuLuan WHERE MaTieuLuan = '" + _maTieuLuan + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                maTieuLuan = table.Rows[0]["MaTieuLuan"].ToString();
                tenTieuLuan = table.Rows[0]["TenTieuLuan"].ToString();
                ngayBaoVe = table.Rows[0].Field<DateTime>("NgayBaoVe");
                noiBaoVe = table.Rows[0]["NoiBaoVe"].ToString();
                nguoiHuongDan = table.Rows[0]["NguoiHuongDan"].ToString();
                ketQuaBaoVe = float.Parse(table.Rows[0]["KetQuaBaoVe"].ToString());
            }
        }
        public Boolean Save()
        {
            String sqlTieuLuan = "INSERT INTO TieuLuan VALUES('"
                + maTieuLuan + "',N'" 
                + tenTieuLuan + "','"
                + ngayBaoVe.Date.ToString("yyyy-MM-dd HH:mm:ss") + "',N'" 
                + noiBaoVe + "',"
                + ketQuaBaoVe + ",N'" 
                + nguoiHuongDan + "')";
            QueryResult result = myDatabase.Query(sqlTieuLuan);

            if (result.ReturnCode == 409)
            {
                return false;
            }
            return true;
        }
        public Boolean Update()
        {
            bool isDupplicated = Database.isDupplicate("TieuLuan", "MaTieuLuan", maTieuLuan);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE TieuLuan SET " 
                    + "TenTieuLuan = '" + tenTieuLuan 
                    + "', NgayBaoVe = '" + Database.sqlDateTime(ngayBaoVe)
                    + "',NoiBaoVe = '" + noiBaoVe 
                    + "',KetQuaBaoVe = " + ketQuaBaoVe 
                    + ",NguoiHuongDan = '" + nguoiHuongDan
                    + "' WHERE MaTieuLuan = '" + maTieuLuan + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    return false;

            }
            return true;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM TieuLuan WHERE MaTieuLuan = '" + maTieuLuan + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            return true;
        }
    }
}
